<?php
/*
	Model Class
	===========
	
	Example:

	$A = new Model("workorder");
	$A->addField("wonum");
	$A->addField("description");
	$A->setID("ID"); // Config the ID field

	$SQL = $A->get_select();
	// return --> select wonum, description from workorder

	$A->setValue("id",1234);
	$SQL = $A->get_update();
	// return --> update workorder set description = "test" where id = "1234"
*/

class Model extends RootClass {

	private $table;
	private $fields;
	private $id;	
	
	// Constructor
	function __construct($table) {
		$this->table = $table;
		$this->fields = array();
		$this->newValues = array();
		$this->where = null;
		$this->groupby = null;
		$this->having = null;
	}
	
	// PENDENCIA DE DESENVOLVIMENTO
	function ModelDB($table,$db,$user,$password) {
	
		// Modelo futuro --> recuperar tipo de dados automaticamente MYSQL
		/* select column_name, 
				  data_type, 
				  character_maximum_length, 
				  is_nullable
			from information_schema.COLUMNS 
			where table_name = 'pesquisa' and 
				  table_schema = 'sigga_pesquisa'
		*/
	}
	
	// generate a new field with alias
	function addField($name,$alias=null,$value=null) {
		$field = new ModelField($name,$alias,$value);	
		$this->fields[count($this->fields)] = $field;
	}
	
	// Set the unique ID for the table
	function setId($ID) {
		$this->id = $ID;
	}
	
	// change a field value
	function setValue($name,$value) {
		for($i=0;$i<count($this->fields);$i++) {
			if($this->fields[$i]->getName() == $name) {
				$this->fields[$i]->setValue($value);
				break;
			}
		}
	}

	// get a field value
	function getValue($name) {
		for($i=0;$i<count($this->fields);$i++) {
			if($this->fields[$i]->getName() == $name) {
				return $this->fields[$i]->getValue();
			}
		}
	}
	
	// Return an array of fields name
	function getFields() {
		$result = array();
		for($i=0;$i<count($this->fields);$i++) {
			$result[$i] = $this->fields[$i]->getName();
			
		}
		return $result;
	
	}
	
	// Return the number of fields
	function numFields() {
		return count(count($this->fields));
	}
	
	// Generate SELECT Query
	function getSelect($id=null) {
		$sql = "select ";
		for($i=0;$i<count($this->fields);$i++) {
			
			if($i == count($this->fields)-1) {
				
				$sql .= $this->fields[$i]->getNameAlias()." ";
			}
			else {
				$sql .= $this->fields[$i]->getNameAlias().", ";
			}
		}
		$sql .= "from ".$this->table;
		
		if($id != null)
			$sql .= " where ".$this->id." = ".$this->getValue($this->id);
	
		return $sql;
	}

	// Generate INSERT DML
	function getInsert() {
		$sql = "insert into ".$this->table." (";
		$sql2 = null;
		for($i=0;$i<count($this->fields);$i++) {
			
			if($i == count($this->fields)-1) {
				$sql  .= $this->fields[$i]->getName()." \n";
				$sql2 .= "'".$this->fields[$i]->getValue()."' \n";
			}
			else {
				$sql .= $this->fields[$i]->getName().", \n";
				$sql2 .= "'".$this->fields[$i]->getValue()."', \n";
			}
		}
		$sql .= ") \nvalues (\n".$sql2.")";
		return $sql;
	}

	// Generate UPDATE DML
	function getUpdate() {
		$sql = "update ".$this->table." set \n";
	
		for($i=0;$i<count($this->fields);$i++) {
			
			if($i == count($this->fields)-1) {
				$sql .= $this->fields[$i]->getName()." = '".$this->fields[$i]->getValue()."' \n";
			}
			else {
				$sql .= $this->fields[$i]->getName()." = '".$this->fields[$i]->getValue()."', \n";
			}
		}		
		$sql .= " where ".$this->id." = ".$this->getValue($this->id);
		return $sql;
	}
	


	// Generate DELET DML
	function getDelete() {
		$sql = "delete from ".$this->table;
		$sql .= " where ".$this->id." = ".$this->getValue($this->id);
		return $sql;
	}
}
?>